package pers.zh.parking.mvp.ui.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import pers.zh.parking.R;
import pers.zh.parking.data.entity.ParkingLot;
import pers.zh.parking.data.entity.ParkingSpace;

public class SpaceAdapter extends BaseQuickAdapter<ParkingSpace, BaseViewHolder> {

    private ParkingLot lot;

    public SpaceAdapter(@Nullable List<ParkingSpace> data, ParkingLot lot) {
        super(R.layout.rv_item_space, data);
        this.lot = lot;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ParkingSpace item) {
        helper.setText(R.id.tvLotName, lot.getName());
        genInfo(helper, item);
        CardView cardView = (CardView)helper.itemView;
        if (item.isToDel()) {
            cardView.setBackgroundResource(R.drawable.bg_long_click);
        } else {
            cardView.setBackgroundResource(R.drawable.bg_no_long_click);
        }
    }

    private void genInfo(BaseViewHolder helper, ParkingSpace item) {
        LinearLayout llRsvInfo = helper.itemView.findViewById(R.id.llRsvInfo);
        // 解决recyclerView复用导致多次调用addView,重复的问题
        llRsvInfo.removeAllViews();
        Map<String, String> map = new LinkedHashMap<>();
        map.put("车位编号", String.valueOf(item.getSpaceNo()));
        map.put("车位状态", String.valueOf(item.getStatusCh()));
        for (Map.Entry<String, String> entry : map.entrySet()) {
            View view = LayoutInflater.from(mContext)
                                      .inflate(R.layout.item_hor_key_value,
                                              (ViewGroup)helper.itemView.getParent(), false);
            TextView tvKey = view.findViewById(R.id.tvKey);
            TextView tvValue = view.findViewById(R.id.tvValue);
            tvKey.setText(entry.getKey());
            tvValue.setText(entry.getValue());
            llRsvInfo.addView(view);
        }
    }
}
